<?php
    @session_start();
require_once 'shared-functions.php';
require_once 'session.php';
require_once 'masterpage.php';

if (!IsValidSession()) {
    header('Location: login.php?page=student-fees');
    exit();
}
else
{
    RefreshSession();
}

$user = GetCurrentUserAccessLevel();

//Check user access
if ($user != $STUDENT) {
    header('Location: login.php?page=student-fees&error=To access to the view student fees page, please log in as a student&logout=1');
    exit();
}

$link = connect_db();

masterpage("Student Fees");
?>
<table class="DataList">
    <tr>
        <th class="header" colspan="5">Payment History</th>
    </tr>
    <tr>
        <th class="header">Date</th>
        <th class="header">Transaction Type</th>
        <th class="header">Amount</th>
        <th class="header">User</th>
        <th class="header">Notes</th>
    </tr>
<?php
        $query = "SELECT `FeeLedger`.`Date`, 
                         `FeeLedger`.`Amount`,
                         `FeeLedger`.`ProcessingUser`,
                         `FeeLedger`.`Comment`,
                         `feeTypes`.`description`
                         FROM `FeeLedger`,`feeTypes` WHERE `StudentID`='" . GetSessionUser() . "' AND `FeeLedger`.`Type`=`feeTypes`.`type` ORDER BY `Date` ASC;";
    $result = mysql_query($query, $link);
    $accountBalance = 0;
    $rowID = 0;
    while ($ledgerEntry = mysql_fetch_array($result, MYSQL_BOTH))
    {

        if ($rowID == 0) {
            $rowClass = "rows";
            $rowID++;
        }
        else
        {
            $rowClass = "altrows";
            $rowID--;
        }

        $accountBalance += $ledgerEntry['Amount'];
        echo '<tr class="' . $rowClass . '">';
        echo '<td>' . date('d-m-Y H:i:s', strtotime($ledgerEntry['Date'])) . "</td>";
        echo '<td>' . $ledgerEntry['description'] . "</td>";
        echo '<td>' . $ledgerEntry['Amount'] . "</td>";
        echo '<td>' . $ledgerEntry['ProcessingUser'] . "</td>";
        echo '<td>' . $ledgerEntry['Comment'] . "</td>";
        echo '</tr>';
    }
    echo '<tr><td class="rows">&nbsp;</td><td class="rows">&nbsp;</td><td class="rows">&nbsp;</td><td class="rows">&nbsp;</td><td class="rows">&nbsp;</td></tr>';
    echo '<tr><td class="rows"></td><td class="rows">Current Balance</td>';
    echo '<td class="rows">' . number_format($accountBalance, 2) . ' GH¢</td><td class="rows"></td><td class="rows"></td></tr>';
    ?>
</table>

<?php
    endmasterpage();
?>

